package com.xueqiu.movieplaymanagersystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xueqiu.movieplaymanagersystem.entity.Visit;
import com.xueqiu.movieplaymanagersystem.mapper.VisitMapper;
import com.xueqiu.movieplaymanagersystem.service.VisitService;

import java.util.List;

/**
 * @author xueqiu
 */
@Service
@Slf4j
public class VisitServiceImpl implements VisitService {
    @Autowired
    private VisitMapper visitMapper;

    /**
     * 新增
     *
     * @param originData 数据源
     * @return 新增成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Visit originData) {
        int insert = visitMapper.insert(originData);
        log.info("VisitServiceImpl save originData ----> {}",originData);
        return insert == 1;
    }

    /**
     * 查询；当前周访问量
     *
     * @return 统计当前周的当前时间的所有访问数据个数；可能为empty
     */
    @Override
    @Transactional(readOnly = true)
    public int findWeekCount() {
        int count = visitMapper.selectWeekCount();
        log.info("VisitServiceImpl findWeekCount count ----> {}",count);
        return count;
    }

    /**
     * 查询；截至目前为止所有访问量
     *
     * @return 统计访问表全表数据个数；可能为emptyv
     */
    @Override
    @Transactional(readOnly = true)
    public int findCount() {
        int count = visitMapper.selectCount();
        log.info("VisitServiceImpl findCount count ----> {}",count);
        return count;
    }

    /**
     * 查询；分页
     *
     * @param page  当前页
     * @param limit 每页条数
     * @return 分页数据；可能为empty
     */
    @Override
    @Transactional(readOnly = true)
    public PageInfo<Visit> findAll(int page, int limit) {
        PageInfo<Visit> resultPageInfo = null;
        try (Page<Visit> originList = PageHelper.startPage(page, limit)) {
            visitMapper.selectAll();
            List<Visit> resultList = originList.getResult();
            resultPageInfo = new PageInfo<>(resultList);
        }
        log.info("VisitServiceImpl findAll resultPageInfo ----> {}",resultPageInfo);
        return resultPageInfo;
    }
}
